package com.forever7776.life.admin.controller.life.m.blog;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.admin.controller.BaseController;
import com.forever7776.life.core.common.vo.UserBlogAdminSearchVO;
import com.forever7776.life.core.common.vo.UserBlogVO;
import com.forever7776.life.core.service.blog.IBlogService;
import com.forever7776.life.core.service.blog.IBlogTypeService;
import com.forever7776.life.remote.common.enums.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import util.ResultUtil;

/**
 * <p>
 * 后台管理系统，博客管理
 * 博客列表
 * </p>
 *
 * @author kz
 * @date 2018-02-28
 */
@Controller
@RequestMapping("/m/blog")
public class ManageBlogDataController extends BaseController {

    @Autowired
    private IBlogService iBlogService;
    @Autowired
    private IBlogTypeService iBlogTypeService;

    @RequestMapping
    public String index(ModelMap map) {
        map.put("types", iBlogTypeService.selectAll(StatusEnum.NORMAL.getKey()));
        return LIFE_ADMIN_VIEW_PAGE_PATH + "blog/blog_user";
    }

    /**
     * 用户博客列表
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject list(UserBlogAdminSearchVO vo) {
        JSONObject result = new JSONObject();
        Page<UserBlogVO> p = new Page<UserBlogVO>();
        p.setCurrent(pageNumber);
        Page<UserBlogVO> page = iBlogService.selectAllUserBlogByAdmin(p, vo);
        result.put("page", page);
        return result;
    }


    /**
     * 根据博客审核状态查询数量
     *
     * @param type
     * @return
     */
    @RequestMapping(value = "/countByType", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject countByType(@RequestParam("type") Integer type) {
        JSONObject result = new JSONObject();
        result.put("count", iBlogService.selectCountByBlogType(type));
        return result;
    }

    /**
     * 修改博客审核状态
     *
     * @param blogId
     * @param type
     * @return
     */
    @RequestMapping(value = "/type/{blogId}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateBlogType(@PathVariable Integer blogId, @RequestParam(value = "type", defaultValue = "1") Integer type) {
        iBlogService.updateType(blogId, type);
        return ResultUtil.getSuccessResult("操作成功！");
    }

    /**
     * 推荐博客
     *
     * @param blogId
     * @param recommendType
     * @return
     */
    @RequestMapping(value = "/recommend/{blogId}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject recommend(@PathVariable Integer blogId, @RequestParam(value = "recommendType", defaultValue = "1") Integer recommendType) {
        iBlogService.updateRecommendType(blogId, recommendType);
        return ResultUtil.getSuccessResult("操作成功！");
    }

}
